package inventory.controler;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.SQLIntegrityConstraintViolationException;

import javax.swing.JOptionPane;

import inventory.daoImplementer.CategoryMasterImplementor;
import inventory.daoImplementer.MaterialMasterImplementor;
import inventory.daoImplementer.MaterialSpecificationImplementor;
import inventory.daoInterface.CategoryMasterDao;
import inventory.daoInterface.MaterialMasterDao;
import inventory.daoInterface.MaterialSpecificationDao;
import inventory.model.CategoryMaster;
import inventory.model.Material;
import inventory.view.MaterialManagement;
import utilites.Constants;
import utilites.ViewUtilites;
import dto.MaterialMaster;
import dto.MaterialSpecification;


/**
 * @author intel
 *
 */
public class MaterialManagementController implements ActionListener,ItemListener
{
	private MaterialManagement view;
	private Material model;
	
	private MaterialMasterDao materialMaster;
	
	
	public MaterialManagementController() {
		
	}


	public MaterialManagementController(MaterialManagement view, Material model) 
	{
		super();
		this.view = view;
		this.model = model;
		this.setViewCombobox();
		
		materialMaster=new MaterialMasterImplementor();
	}

	public MaterialManagement getView() 
	{
		return view;
	}


	public void setView(MaterialManagement view) 
	{
		this.view = view;
	}


	public Material getModel() {
		return model;
	}


	public void setModel(Material model) 
	{
		this.model = model;
	}
	
	public void reactOnbtnAdd()
	{
		/*
		 * This method add new rows on table Material_Master and Material_specification.
		 */
		
		String materialName=view.getAddTfName().getText().trim();
		String categoryName=view.getAddCbCategory().getSelectedItem().toString();
		
		CategoryMasterDao impl=new CategoryMasterImplementor();
		CategoryMaster cat=impl.retreive("catName",categoryName);
		
		String catCode=cat.getCatCode();
		
		String unit=view.getAddTfUnit().getText().trim();
		String mspecs=view.getAddTfSpecification().getText().trim();
		
		MaterialMaster master=new MaterialMaster(catCode+materialName,materialName,catCode,Constants.ACTIVE,unit);
		
		MaterialSpecification spec=new MaterialSpecification(catCode+materialName,mspecs);
		model=new Material(master,spec);
		
		MaterialSpecificationDao materialSpecification=new MaterialSpecificationImplementor();
				
		try
		{
		
			materialSpecification.add(model.getSpec());
			materialMaster.add(model.getMaterial());
			JOptionPane.showMessageDialog(null, "Data successfully added");
		}
		
		catch(SQLIntegrityConstraintViolationException e)
		{
			JOptionPane.showMessageDialog(null, "Material Name already exist, please use another one");
		}
		
		catch(Exception e)
		{
			JOptionPane.showMessageDialog(null, "Data not successfully added,Please try again!");
		
		}
	  }
	
	
	public void reactOnButtonUpdate()
	{
		String name=view.getEditcbMaterialName().getSelectedItem().toString();
		String category=view.getEditCbCategory().getSelectedItem().toString();
		String newName=view.getEditTfNewName().getText();
		String unit=view.getEditTfUnit().getText();
		String mspecs=view.getEditCbSpecification().getSelectedItem().toString();
		
		CategoryMasterDao impl=new CategoryMasterImplementor();
		CategoryMaster cat=impl.retreive("catName",category);
		String code=cat.getCatCode();
		
		MaterialMasterDao imp= new MaterialMasterImplementor();
		MaterialMaster master=imp.retreive("mName",name);
		
		master.setmName(newName);
		master.setCatCode(code);
		
		if(view.getRdbtnActive().isSelected())
			master.setStatus(Constants.ACTIVE);
		
		if(view.getRdbtnInactive().isSelected())
			master.setStatus(Constants.INACTIVE);
		
		try
		{
			imp.update(master);
			JOptionPane.showMessageDialog(null, "Data successfully Updated");
		}
		
		catch(Exception e)
		{
			JOptionPane.showMessageDialog(null, "Data not successfully Updated,Please try again!");
		
		}
		
        MaterialSpecificationDao ims=new MaterialSpecificationImplementor();
        MaterialSpecification s=ims.retreive("mCode", master.getmCode());
        
		try
		{
			ims.update(s);
			JOptionPane.showMessageDialog(null, "Data successfully Updated");
		}
		
		catch(Exception e)
		{
			JOptionPane.showMessageDialog(null, "Data not successfully Updated,Please try again!");
		
		}	
	}
	
	private void setViewCombobox()
	{
		CategoryMasterDao implementor=new CategoryMasterImplementor();
		
		CategoryMaster[] category=implementor.retreiveAll();
		
		String[] items=new String[category.length+1];
		
		items[0]="--Select Category--";
		
		for(int i=1;i<=items.length;i++)
		   items[i]=category[i-1].getCatName();
		
		ViewUtilites.set_comboBox(view.getAddCbCategory(), items);
		ViewUtilites.set_comboBox(view.getEditCbCategory(), items);
	}
	
	public void fillComboBoxMaterialName()
	{
		String[] matNames=null;
		MaterialMaster[] mm=null;
		
		mm=materialMaster.retreiveAll();
		
		matNames=new String[mm.length+1];
		
		matNames[0]="--Select Name--";
		
		for(int i=1;i<=mm.length;i++)
		{
			matNames[i]=mm[i-1].getmName();
		}
		
		ViewUtilites.set_comboBox(view.getEditcbMaterialName(),matNames);
		ViewUtilites.setAutoSuggestionsComboBox(view.getEditcbMaterialName());
	}
	
	public void setInfo(String itemName)
	{
		MaterialMaster mm=materialMaster.retreive("catName",itemName);
		
		String catCode=mm.getCatCode();
		CategoryMasterDao cmd=new CategoryMasterImplementor();
		CategoryMaster cm=cmd.retreive("catCode",catCode);
		String catName=cm.getCatName();
		
		view.getEditCbCategory().setSelectedItem(catName);
		view.getEditTfUnit().setText(mm.getmUnit());
		
		fillComboBoxMaterialSpecification();
		
		if(mm.getStatus()==Constants.ACTIVE)
		{
			view.getRdbtnActive().setSelected(true);
		}
		
		if(mm.getStatus()==Constants.INACTIVE)
		{
			view.getRdbtnInactive().setSelected(true);
		}
	}
	
	public void fillComboBoxMaterialSpecification()
	{
		String[] specifications=null;
		MaterialSpecification[] ms=null;
			
		String matName=view.getEditcbMaterialName().getSelectedItem().toString();
		
		MaterialMasterDao mmd=new MaterialMasterImplementor();
		MaterialMaster m=mmd.retreive("mName",matName);
		String mCode=m.getmCode();
		
		MaterialSpecificationDao msd=new MaterialSpecificationImplementor();
		ms=msd.reteriveArray("mCode",mCode);
		
		specifications=new String[ms.length+1];
		
		specifications[0]="--Select Specifications--";
		
		for(int i=1;i<=ms.length;i++)
		{
			specifications[i]=ms[i-1].getmSpecs();
		}
		
		ViewUtilites.set_comboBox(view.getEditcbMaterialName(),specifications);
		ViewUtilites.setAutoSuggestionsComboBox(view.getEditCbSpecification());
	}

	//*******************************************************************
	@Override
	public void itemStateChanged(ItemEvent event) 
	{
		if (event.getStateChange() == ItemEvent.SELECTED) 
		{
	          Object item = event.getItem();
	          String itemName=item.toString();
	          setInfo(itemName);
	    }
	}


	@Override
	public void actionPerformed(ActionEvent arg0) 
	{
		String k=arg0.getActionCommand();
		
		if(k.contains("add"))
		{
			reactOnbtnAdd();
		}
		if(k.contains("update"))
		{
			reactOnButtonUpdate();
		}
	}
}
